package com.example.industrialpipelinemonitoring.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.industrialpipelinemonitoring.bean.User;
import com.example.industrialpipelinemonitoring.dao.UserDao;
import com.example.industrialpipelinemonitoring.dto.LoginDTO;
import com.example.industrialpipelinemonitoring.dto.UserListDTO;
import com.example.industrialpipelinemonitoring.utils.AjaxResult;
import com.example.industrialpipelinemonitoring.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Dsy
 * @create 2021-11-04  20:45
 */
@Service
public class UserService{

    @Autowired
    private UserDao userDao;

    public AjaxResult login(LoginDTO dto) {
        User user = userDao.selectOne(new QueryWrapper<User>().eq("username", dto.getUsername()).eq("password", dto.getPassword()));
        if(user != null) {
            // 如果登录成功 则返回token （token内有id）
            return AjaxResult.success(JwtUtils.getTokenByUId(user.getId().toString()));
        }else {
            return AjaxResult.fail("用户名或密码错误");
        }
    }

    public AjaxResult listAll() {
        List<UserListDTO> list = userDao.list();
        return AjaxResult.success(list);
    }

    public List<UserListDTO> listAll2() {
        return userDao.list();
    }
}
